package learn_concurrent._8;

import java.util.concurrent.locks.LockSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class T5 {
	static final Logger logger = LoggerFactory.getLogger(T5.class);
	
	static Thread t1;
	static Thread t2;
	static Thread t3;
	public static void main(String[] args) throws InterruptedException {
		ParkUnpark pu = new ParkUnpark(5);
		t1 = new Thread(() -> pu.print("a", t2));
		t2 = new Thread(() -> pu.print("b", t3));
		t3 = new Thread(() -> pu.print("c", t1));

		t1.start();
		t2.start();
		t3.start();
		
		LockSupport.unpark(t1);
	}

}

class ParkUnpark {
	private final int loopNum;
	
	public ParkUnpark(int loopNum) {
		this.loopNum = loopNum;
	}
	
	public void print(String str, Thread next) {
		for (int i = 0; i < loopNum; i++) {
			LockSupport.park();
			System.out.print(str);
			LockSupport.unpark(next);
		}
	}
}
